import React from 'react';
import {StyleSheet, View} from 'react-native';
import Toast from 'react-native-easy-toast';

import auth from '../../../common/auth';

import Header from '../../../components/header';
import Form from './form';
import api from '../../../common/api';
import utils from "../../../common/utils";

export default class extends React.Component {
    state = {
        userInfo: {},
        isFetching: false,
        time: 0,
        isCodeGetting: false
    };

    //提交
    submit = (pwd, verify_code, new_pwd) => {
        if (this.state.isFetching) {
            return;
        }
        if (!pwd) {
            this.showToast('请填写旧密码');
            return;
        }
        if (!verify_code) {
            this.showToast('请填写验证码');
            return;
        }
        if (!new_pwd) {
            this.showToast('请填写新密码');
            return;
        }
        this.setState({
            isFetching: true
        });
        api({url: 'api/member/changePwd', data: {pwd, verify_code, new_pwd}})
            .then(() => {
                auth.goBack();
            })
            .catch(e => {
                this.showToast(e.msg);
            })
            .finally(() => {
                this.setState({
                    isFetching: false
                });
            });
    };

    //发送手机验证码
    sendSms = () => {
        const {time, isCodeGetting, userInfo: {phone}} = this.state;
        if (time || isCodeGetting) {
            return;
        }
        if (!this.checkPhone(phone)) {
            return;
        }
        this.setState({
            isCodeGetting: true
        });
        api({url: 'api/member/getCodeByPhone', data: {phone, use_type: 3}})
            .then(() => {
                this.timeDown();
            })
            .catch(e => {
                this.showToast(e.msg);
            })
            .finally(() => {
                this.setState({
                    isCodeGetting: false
                });
            });
    };
    //检查手机号
    checkPhone = (phone) => {
        if (!phone) {
            this.showToast('请填写手机号');
            return false;
        }
        if (!utils.checkMobile(phone)) {
            this.showToast('手机号码格式不正确');
            return false;
        }
        return true;
    };
    //倒计时
    timeDown = () => {
        const timeout = 10;
        this.setState({
            time: timeout
        });
        this.timer = setInterval(
            () => {
                if (this.state.time <= 0) {
                    clearInterval(this.timer);
                    return;
                }
                this.tick();
            },
            1000
        );
    };
    //倒计时
    tick = () => {
        this.setState(prevState => ({
            time: prevState.time - 1
        }));
    };

    //toast弹出
    showToast = (text, time = 1000) => {
        this.refs.toast.show(text, time);
    };

    componentDidMount() {
        auth.getUserInfo()
            .then(userInfo => {
                this.setState({
                    userInfo: userInfo
                });
            })
            .catch(() => {
                auth.goBack();
            });
    }

    componentWillUnmount() {
        this.timer && clearInterval(this.timer);
    }

    render() {
        const {userInfo, isCodeGetting, time} = this.state;
        return (
            <View style={styles.container}>
                <Header title="修改密码"/>
                <Form userInfo={userInfo}
                      submit={this.submit}
                      sendSms={this.sendSms}
                      isCodeGetting={isCodeGetting}
                      time={time}/>
                <Toast ref="toast" position="center"/>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        backgroundColor: '#f0f0f0',
        flex: 1
    }
});
